import {md5} from './md5';
var app = getApp();


// 统计页面函数
function statistic(params={}) {
    let url = getCurrentPageUrl();
    var timestamp = Date.parse(new Date());
    let SystemInfo = wx.getSystemInfoSync();
        SystemInfo.page = url,
        SystemInfo.sign = wx.getStorageSync('sign');
        SystemInfo.time = timestamp;
        SystemInfo.app = 'hepulanshop'; //moqikaoyandazhan
        SystemInfo.token = md5('fpSndI0m7XQGenfK#' + timestamp);
    params.data = SystemInfo;
}

function getOpenid(){
  let that = this;
  return new Promise((resolve,reject)=>{
    wx.login({
      success(res){
        console.log("我是统计",res)
        wx.request({
          // url: app.url+`/index.php/mobile/Shopindex/login?code=${res.code}`,
          url: app.urlData.getURL + "/index.php/api/SApi/index/key/" + app.urlData.key + "/m/actionAuth/wid/" + app.urlData.wid,
          data: {
            code: res.code
          },
          success(res){
            // console.log("2", res)
            resolve(res.data)
          }
        })
      }
    })
  })
}


var openIdArr = [{
  name:'熊思琦',
  openid: 'or-Qf0a7YvBFviUQetv9EWVzI0X8',
  scene:'2c832ea31b2609c9afbf49572776066a'
}, {
  name: '晓晴 ',
  openid: 'or-Qf0W1sHxi2tIAddYnVqo-XIWY',
  scene: '217557e94685e89cfa2b2e82113c92f2'
}]

function getUserScene(openIdArr){
  getOpenid().then(res=>{
    openIdArr.map(function (el) {
      if (el.openid == res.openid) {

      }
    })
  })
}

// 统计用户来源
function fromPageData(params={}) {
    var timestamp = Date.parse(new Date());
    params.data = params.data ? params.data:{};
    params.data = {
      openid: params.openid,
      scene: params.scene,
      sign: params.sign,
      is_fresh: params.is_fresh,//是否是新用户
      time: timestamp,
      token: md5('fpSndI0m7XQGenfK#' + timestamp),
      app: 'hepulanshop'
    }
    console.log("3",params.data)

    wx.request({
      url: 'https://tj.zealcdn.cn/?_a_=serverReport',
      data: params.data,
      method: 'POST',
      success: function (res) {
        console.log("4", res)
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })

}

//咨询上报统计
function fromConsultation(params = {}) {
  var timestamp = Date.parse(new Date());
  params.data = params.data ? params.data : {};
  params.data = {
    event: 'zixun',
    sign: wx.getStorageSync('sign'),
    time: timestamp,
    token: md5('fpSndI0m7XQGenfK#' + timestamp),
    app: 'hepulanshop'
  }
  console.log("咨询的params", params.data)

  wx.request({
    url: 'https://tj.zealcdn.cn/?_a_=clientEvent',
    data: params.data,
    method: 'POST',
    success: function (res) {
      console.log("咨询success", res)
    },
    fail: function () {
      console.log("咨询fail", res)
    }
  })

}


// function updateConsultation() {
//   getOpenid().then((res) => {
//     if (scenes) {
//       fromPageData({ scene: scenes, openid: res.openid, sign: res.sign, is_fresh: res.is_fresh })
//     } else {
//       for (let i = 0; i < openIdArr.length; i++) {
//         // console.log(i,res)
//         if (openIdArr[i].openid == res.openid) {
//           console.log("2", openIdArr[i].scene)
//           wx.setStorageSync('scene', openIdArr[i].scene)
//         }
//       }
//     }
//   })
// }



function updateOpenId(scenes){
  getOpenid().then((res) => {
    if (scenes){
      fromPageData({ scene: scenes, openid: res.openid, sign: res.sign, is_fresh: res.is_fresh })
    } else {
      for (let i = 0; i < openIdArr.length; i++) {
        // console.log(i,res)
        if (openIdArr[i].openid == res.openid) {
          console.log("2",openIdArr[i].scene)
          wx.setStorageSync('scene', openIdArr[i].scene)
        }
      }
    }
  })
}



module.exports = {
    statistic,
    fromPageData,
    getOpenid,
    updateOpenId,
    fromConsultation
}
